#ifndef QQ_HANDLE
#define QQ_HANDLE

#include <qobject.h>
#include <qtcpsocket.h>
#include <qjsonobject.h>
#include <qjsondocument.h>
#include <qlist.h>
#include <qcryptographichash.h>
#include <qtimer.h>

#include <jwt-cpp/jwt.h>

#include "QQSql.h"

class QQHandle : public QObject
{
	Q_OBJECT
public:
	static QQHandle* get_instance();
	void add_new_socket(QTcpSocket* socket);
	QString generate_token(QVariantList& list);
private:
	QQHandle(QObject* parent = nullptr);
	~QQHandle();

private slots:
	void do_ready_read();
	void do_tcp_error(QAbstractSocket::SocketError error);
	void do_tcp_disconnected();
private:
	void handle_login(QJsonObject& request, QTcpSocket* socket);
	void handle_register(QJsonObject& request, QTcpSocket* socket);
	void handle_add_friend(QJsonObject& request, QTcpSocket* socket);
	void handle_send_message(QJsonObject& request, QTcpSocket* socket);
private:
	static QQHandle* m_instance;
	QMap<QTcpSocket*, QString> m_sockets;
	QMap<QString, QTcpSocket*> m_accounts;
};

#endif // !QQ_HANDLE
